﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml;

namespace SubstrateHelper.Helpers
{
    public class XmlHelper
    {
        public static string ModifyXml(string pathXml, List<ProcessBase.Models.XmlModifyTemplate> templates, ProcessBase.Models.Order order, object inputValue, ProcessBase.Models.V_MES_HX_WR wr = null)
        {
            var doc = XDocument.Load(pathXml);

            foreach (var template in templates)
            {
                string itemName;
                var item = GetXObject(template, doc, out itemName);
                string value = "";
                //
                if (template.ValueSource.StartsWith("固定"))
                {
                    value = template.FixValue;
                }
                else if (template.ValueSource.StartsWith("自生成"))
                {
                    var type = inputValue.GetType();
                    var prop=type.GetProperty(itemName);
                    value = prop.GetValue(inputValue)?.ToString() ?? "";
                }
                else//来自NC库查询
                {
                    string[] strs = template.Value.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                    var src = strs[0].ToUpper();
                    if (src == "V_MES_HX_MO")
                    {
                        var propName = strs[1].ToLower();//V_MES_HX_MO属性全是小写
                        var type = order.GetType();
                        var prop = type.GetProperty(propName);
                        value = prop.GetValue(order)?.ToString() ?? "";
                    }
                    else//V_MES_HX_WR
                    {
                        var propName = strs[1].ToUpper();//V_MES_HX_WR属性全是大写
                        var type = wr.GetType();
                        var prop = type.GetProperty(propName);
                        value = prop.GetValue(wr)?.ToString() ?? "";
                    }
                }

                //
                var nodeType = item.NodeType;
                if (nodeType == XmlNodeType.Element)//元素
                {
                    var element = item as XElement;
                    element.Value = value ?? "";
                }
                else//属性
                {
                    var attr = item as XAttribute;
                    attr.Value = value ?? "";
                }
            }
            return doc.ToString();
        }


        public static XObject GetXObject(ProcessBase.Models.XmlModifyTemplate template, XDocument doc,out string itemName)
        {
            string[] strs = template.Item.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
            var item = doc.XPathSelectElement(strs[0]);
            if (strs.Length < 2)
            {
                strs = strs[0].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                itemName = strs[strs.Length - 1];
                return item;//返回元素
            }
            else
            {
                var attr = item.Attribute(XName.Get(strs[1]));
                itemName = strs[1];
                return attr;//返回属性
            }

        }
    }
}
